package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Appraise;
import cn.sunlelin.educationserver.mapper.AppraiseMapper;
import cn.sunlelin.educationserver.service.AppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AppraiseServiceImpl implements AppraiseService {
    @Autowired
    private AppraiseMapper appraiseMapper;
    @Override
    public Boolean createAppraise(Appraise appraise) {
        if(this.selectAppraiseByCourseAndUser(appraise)!=null)
            return false;
        appraise.setCreateTime(new Date());
        appraiseMapper.createAppraise(appraise);
        return true;
    }

    @Override
    public Boolean updateAppraise(Appraise appraise) {
        if(this.selectAppraiseByCourseAndUser(appraise)==null)
            return false;
        appraise.setUpdateTime(new Date());
        appraiseMapper.updateAppraise(appraise);
        return true;
    }

    @Override
    public List<Map<String,Object>> selectAppraiseByCourseId(Integer courseId) {
        return appraiseMapper.selectAppraiseByCourseId(courseId);
    }

    @Override
    public List<Appraise> selectAppraiseByUserId(Integer userId) {
        return appraiseMapper.selectAppraiseByUserId(userId);
    }

    @Override
    public Appraise selectAppraiseById(Integer appraiseId) {
        return appraiseMapper.selectAppraiseById(appraiseId);
    }

    @Override
    public Appraise selectAppraiseByCourseAndUser(Appraise appraise) {
        return appraiseMapper.selectAppraiseByCourseAndUser(appraise);
    }

    @Override
    public Boolean deleteAppraise(Integer appraiseId) {
        if(this.selectAppraiseById(appraiseId)==null)
            return false;
        Appraise appraise=new Appraise();
        appraise.setAppraiseId(appraiseId);
        appraise.setUpdateTime(new Date());
        appraiseMapper.deleteAppraise(appraise);
        return true;
    }
}
